<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User
 *
 * @author Windy
 */
class Core_Models_User extends Rabbit_Db_Table_Abstract implements Core_Models_Interfaces_User {

    protected $_name = 'core_user';

    public function convert($params){
        return null;
    }
    public function getAllUsers() {
        return $this->fetchAll();
    }

    /**
     * Insert new user
     * @param array $params Data of new user
     * @return int $id Id of new user
     */
    public function add($params) {
        $user = $this->createRow($params);
        $user->save();
        return $user->id;
    }
    public function edit($params){
        try{
            $this->update($params, 'id = '.$params['id']);
            return true;
        }catch(Exception $ex){
            return false;
        }
    }
    public function delete($id){
            return parent::delete('id = '. $id);
    }

    public function authenticate($username, $password) {
        try {
            $db = $this->getAdapter();
            $select = $this->getAdapter()->select()
                            ->from(array('u' => $this->_name))
                            ->joinLeft(
                                    array('r' => $this->_prefix . 'core_role'),
                                    'u.role_id = r.id',
                                    array('r.name as role_name'))
                            ->where('username = ?', $username)
                            ->where('password = ?', $password)
                            ->limit(1);
            $stm = $db->query($select);
            return $stm->fetchObject();
        } catch (Exception $ex) {
            echo $ex->getMessage();
            return null;
        }
    }

    public function getAll() {
        try {
            return $this->getAdapter()->select()
                    ->from(array('u' => $this->_name))
                    ->joinLeft(
                            array('r' => $this->_prefix . 'core_role'),
                            'u.role_id = r.id', array('role_name' => 'r.name')
                    )
                    ->query()->fetchAll();
        } catch (Exception $ex) {
            echo $ex->getMessage();
            return null;
        }
    }
    /**
     * Get user by id
     * @param int $id Id of user
     * @return Zend_Db_Table_Row
     */
    public function getById($id){
        return $this->find($id)->current();
    }

    /**
     * Check value of user is existing or not
     * @param string $checkFor Name of field
     * @param string $value Value to check
     * @return bool
     */
    public function exist($checkFor, $value) {
        try {
            $select = $this->getAdapter()->select()->from($this->_name, array('num_users' => 'COUNT(id)'));
            switch ($checkFor) {
                case 'username':
                    $select->where('username = ?', $value);
                    break;
                case 'email':
                    $select->where('email = ?', $value);
                    break;
            }
            $numUsers = $select->limit(1)->query()->fetch()->num_users;
            return ($numUsers == 0) ? false : true;
        } catch (Exception $ex) {
            echo $ex->getMessage();
            return false;
        }
    }

    /**
     * Change status of user
     * @param int $id Id of user
     * @return int
     */
    public function toggleStatus($id) {
        return $this->update(array('actived' => new Zend_Db_Expr('1 - actived')), 'id = ' . $id);
    }

}

?>